package astra.client.pages.clients;

import astra.client.models.Client;
import astra.client.pages.Page;
import astra.client.utils.widgets.table.ClickableTextColumn;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.view.client.ListDataProvider;

/**
 * @author Kotelnikov Dmitry <a href="mailto:dimonnot@mail.ru"/>
 */
public class ClientsPage extends Page{
    interface ClientsTableUiBinder extends UiBinder<HTMLPanel, ClientsPage> {
    }

    private static ClientsTableUiBinder ourUiBinder = GWT.create(ClientsTableUiBinder.class);

    @UiField(provided = true)
    CellTable table;
    @UiField
    Button showAddClientDialog;
    @UiField
    FlowPanel actions;

    private ListDataProvider<Client> dataProvider;

    @UiConstructor
    public ClientsPage(String name) {
        super(name);



        table = new CellTable<Client>();

        table.addColumn(
            new TextColumn<Client>() {
                @Override
                public String getValue(Client object) {
                    return object.getName();
                }
            },
            "Название"
        );
        table.addColumn(
                new TextColumn<Client>() {
                    @Override
                    public String getValue(Client object) {
                        return object.getAddress();
                    }
                },
                "Адрес"
        );
        table.addColumn(
            new TextColumn<Client>() {
                @Override
                public String getValue(Client object) {
                    return object.getPhone();
                }
            },
            "Телефон"
        );
        table.addColumn(
            new ClickableTextColumn<Client>() {
                @Override
                public String getValue(Client object) {
                    return "изменить";
                }

                @Override
                public void onBrowserEvent(Cell.Context context, Element elem, Client object, NativeEvent event) {
                    astra.getClientPopup().edit(object);
                }
            },
            ""
        );

        dataProvider = new ListDataProvider<Client>();
        dataProvider.addDataDisplay(table);

        initWidget(ourUiBinder.createAndBindUi(this));
    }

    @Override
    public void show() {
        showActions(actions);
        dataProvider.setList(astra.getAstraData().getClients());
    }

    public void refresh() {
        dataProvider.refresh();
    }

    @UiHandler("showAddClientDialog")
    public void onClickShowClientDialog(ClickEvent event) {
        astra.getClientPopup().edit(null);
    }

}